package com.ontheroad.controller;

import cn.modoumama.page.GridDataModel;
import com.ontheroad.banner.model.BannerModel;
import com.ontheroad.banner.service.BannerService;
import com.ontheroad.core.annotation.QiniuUploadToken;
import com.ontheroad.core.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

/**
 * 轮播图
 * Created by Administrator on 2017/10/25.
 */
@Controller
@RequestMapping("banner/")
public class BannerController extends BaseController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private BannerService bannerService;

    @Value("${context_url}")
    private String contextUrl;

    @RequestMapping("init")
    public String init() {
        return "banner/init";
    }

    @RequestMapping("getData")
    @ResponseBody
    public GridDataModel getData(HttpServletRequest request) {
        return getGridData(request, bannerService);
    }

    @QiniuUploadToken
    @RequestMapping("toAdd")
    public String toAdd(Map map) {
        return "banner/add";
    }

    @RequestMapping("save")
    public String save(BannerModel banner, Map map,HttpServletRequest request) {
        try {
            banner.setCreateTime(new Date());
            banner = bannerService.save(banner);
            banner.setUrl(contextUrl + "/banner/detail?id=" + banner.getId());
            bannerService.updateById(banner);
        } catch (Exception e) {
            log.error("保存banner出错：", e);
            map.put("optionResult", 2);
            map.put("faildMessage", "保存banner出错");
        }
        return SUCCESS;
    }

    @QiniuUploadToken
    @RequestMapping("edit")
    public String edit(Integer id, Map map) {
        BannerModel banner = bannerService.findById(id);
        if (banner == null) {
            return FAILD;
        }
        map.put("banner", banner);
        return "banner/edit";
    }

    @RequestMapping("update")
    public String update(BannerModel banner, Map map) {
        try {
            BannerModel old = bannerService.findById(banner.getId());
            Integer publishCount = bannerService.getPublishCount();
            if (publishCount >= 6 && !old.getIsPublish() && banner.getIsPublish()) {
                // 清除最先发布的banner
                BannerModel first = bannerService.findFirstPublishBanner();
                first.setIsPublish(false);
                bannerService.updateById(first);
            }
            bannerService.updateById(banner);

        } catch (Exception e) {
            log.error("保存banner出错：", e);
            map.put("optionResult", 2);
            map.put("faildMessage", "保存banner出错");
        }
        return SUCCESS;
    }

    @RequestMapping(value = "delete")
    public void delete(HttpServletRequest request, PrintWriter out) {
        log.debug("method: delete() ");
        String msg = "操作成功";
        boolean result = true;
        try {
            String ids = request.getParameter("ids");
            String[] aDeleteId = ids.split(",");
            for (String id : aDeleteId) {
                bannerService.removeById(Integer.valueOf(id));
            }
        } catch (Exception e) {
            msg = "系统发生异常！";
            result = false;
        }
        ajaxJsonResponse(out, result, msg);
    }


    @RequestMapping("detail")
    public String detail(Map map, Integer id) {
        BannerModel bannerModel = bannerService.findById(id);
        map.put("banner", bannerModel);
        return "banner/detail";
    }

}
